{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "623323981a510ca1",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b776b945d84c5f66",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T15:09:27.033064Z",
     "start_time": "2024-05-27T15:09:10.329197Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n",
      "Requirement already satisfied: openai in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (1.30.5)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (0.27.0)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (4.4.0)\n",
      "Requirement already satisfied: tqdm>4 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (4.66.4)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (2.7.2)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.7 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (4.12.0)\n",
      "Requirement already satisfied: sniffio in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from openai) (1.3.1)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from anyio<5,>=3.5.0->openai) (3.7)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from anyio<5,>=3.5.0->openai) (1.2.1)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from httpx<1,>=0.23.0->openai) (1.0.5)\n",
      "Requirement already satisfied: certifi in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from httpx<1,>=0.23.0->openai) (2024.2.2)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n",
      "Requirement already satisfied: pydantic-core==2.18.3 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from pydantic<3,>=1.9.0->openai) (2.18.3)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip3.10 install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n",
      "Defaulting to user installation because normal site-packages is not writeable\n",
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple/\n",
      "Requirement already satisfied: langchain in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (0.2.1)\n",
      "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (4.0.3)\n",
      "Requirement already satisfied: pydantic<3,>=1 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (2.7.2)\n",
      "Requirement already satisfied: requests<3,>=2 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (2.32.3)\n",
      "Requirement already satisfied: langchain-text-splitters<0.3.0,>=0.2.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (0.2.0)\n",
      "Requirement already satisfied: langchain-core<0.3.0,>=0.2.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (0.2.3)\n",
      "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (8.3.0)\n",
      "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (3.9.5)\n",
      "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (2.0.30)\n",
      "Requirement already satisfied: PyYAML>=5.3 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (6.0.1)\n",
      "Requirement already satisfied: langsmith<0.2.0,>=0.1.17 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (0.1.67)\n",
      "Requirement already satisfied: numpy<2,>=1 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain) (1.26.4)\n",
      "Requirement already satisfied: attrs>=17.3.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (23.2.0)\n",
      "Requirement already satisfied: yarl<2.0,>=1.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.4)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.4.1)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.5)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n",
      "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain-core<0.3.0,>=0.2.0->langchain) (1.33)\n",
      "Requirement already satisfied: packaging<24.0,>=23.2 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langchain-core<0.3.0,>=0.2.0->langchain) (23.2)\n",
      "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from langsmith<0.2.0,>=0.1.17->langchain) (3.10.3)\n",
      "Requirement already satisfied: typing-extensions>=4.6.1 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from pydantic<3,>=1->langchain) (4.12.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from pydantic<3,>=1->langchain) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.18.3 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from pydantic<3,>=1->langchain) (2.18.3)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from requests<3,>=2->langchain) (2.2.1)\n",
      "Requirement already satisfied: idna<4,>=2.5 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from requests<3,>=2->langchain) (3.7)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from requests<3,>=2->langchain) (3.3.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from requests<3,>=2->langchain) (2024.2.2)\n",
      "Requirement already satisfied: jsonpointer>=1.9 in /Users/wenzhixin/Library/Python/3.10/lib/python/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3.0,>=0.2.0->langchain) (2.4)\n",
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.0.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.0\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip3.10 install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install openai\n",
    "%pip install langchain\n",
    "import os\n",
    "import getpass\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"输入openAi-key\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a634d37737884a8d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 1.Prompt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "32299a2b6c864da0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T15:24:27.987073Z",
     "start_time": "2024-05-27T15:24:25.277495Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='描述中有一个错误，因为星期二之后是星期三，而不是星期六。'\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "llms = ChatOpenAI(model = \"gpt-3.5-turbo\")\n",
    "\n",
    "prompt = \"\"\"\n",
    "    今天星期二，明天星期六\n",
    "    请问上面描述有什么问题?\n",
    "\"\"\"\n",
    "response = llms.invoke(prompt)\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfd14cc1bcd07b09",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2.PromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "7376e99662c49343",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T15:51:04.846926Z",
     "start_time": "2024-05-27T15:51:01.961810Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "组合后的用户请求: \n",
      "你作为导游帮我回答如下问题，回答不超过两句\n",
      "我想去北京旅游，有什么好推荐的？\n",
      "content='您可以参观长城、故宫、颐和园等著名景点，尝试北京烤鸭和老北京炸酱面。'\n"
     ]
    }
   ],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "llms = ChatOpenAI(model = \"gpt-3.5-turbo\")\n",
    "#下面的{location} 作为替换符\n",
    "#加入了instruction, 让大模型知道提问的背景，让它知道调用旅游方面的能力\n",
    "\n",
    "template = \"\"\"\n",
    "你作为导游帮我回答如下问题，回答不超过两句\n",
    "我想去{location}旅游，有什么好推荐的？\n",
    "\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"location\"],\n",
    "    template=template\n",
    ")\n",
    "#这里是真正的用户输入 \"北京\"\n",
    "final_prompt = prompt.format(location = \"北京\")\n",
    "\n",
    "print(f\"组合后的用户请求: {final_prompt}\")\n",
    "\n",
    "response = llms.invoke(final_prompt)\n",
    "print(response)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "126344db716bf52f",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 3.Output Parser\n",
    "格式指令 - 一个自动生成的提示，告诉LLM如何根据你期望的结果格式化它的响应\n",
    "解析器 - 一种将模型的文本输出提取到期望的结构（通常是json）的方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "385fe6bcde2088b1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T16:08:52.495317Z",
     "start_time": "2024-05-27T16:08:52.488611Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The output should be a markdown code snippet formatted in the following schema, including the leading and trailing \"```json\" and \"```\":\n",
      "\n",
      "```json\n",
      "{\n",
      "\t\"错误的文字\": string  // 这个是错误的文字\n",
      "\t\"正确的文字\": string  // 这个是正确的文字\n",
      "}\n",
      "```\n"
     ]
    }
   ],
   "source": [
    "from langchain.output_parsers import StructuredOutputParser,ResponseSchema\n",
    "from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "llms = ChatOpenAI(model = \"gpt-3.5-turbo\")\n",
    "\n",
    "#定义输出的结构\n",
    "response_schema = [\n",
    "    ResponseSchema(name=\"错误的文字\",description=\"这个是错误的文字\"),\n",
    "    ResponseSchema(name=\"正确的文字\",description=\"这个是正确的文字\")\n",
    "]\n",
    "\n",
    "#定义输出解析器\n",
    "output_parser = StructuredOutputParser.from_response_schemas(response_schema)\n",
    "\n",
    "#获取输出指令\n",
    "format_instructions = output_parser.get_format_instructions()\n",
    "print(format_instructions)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "6455d51545e5f06d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-27T16:19:38.902017Z",
     "start_time": "2024-05-27T16:19:35.197051Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "content='```json\\n{\\n\\t\"错误的文字\": \"天启\",\\n\\t\"正确的文字\": \"天气\"\\n}\\n{\\n\\t\"错误的文字\": \"离\",\\n\\t\"正确的文字\": \"里\"\\n}\\n{\\n\\t\"错误的文字\": \"玩\",\\n\\t\"正确的文字\": \"玩\"\\n}\\n```'\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "expected string or bytes-like object, got 'dict'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[43], line 23\u001b[0m\n\u001b[1;32m     20\u001b[0m \u001b[38;5;28mprint\u001b[39m(response)\n\u001b[1;32m     21\u001b[0m \u001b[38;5;66;03m#格式化结果\u001b[39;00m\n\u001b[0;32m---> 23\u001b[0m \u001b[43moutput_parser\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mparse\u001b[49m\u001b[43m(\u001b[49m\u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mto_json\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/output_parsers/structured.py:96\u001b[0m, in \u001b[0;36mStructuredOutputParser.parse\u001b[0;34m(self, text)\u001b[0m\n\u001b[1;32m     94\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mparse\u001b[39m(\u001b[38;5;28mself\u001b[39m, text: \u001b[38;5;28mstr\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Any:\n\u001b[1;32m     95\u001b[0m     expected_keys \u001b[38;5;241m=\u001b[39m [rs\u001b[38;5;241m.\u001b[39mname \u001b[38;5;28;01mfor\u001b[39;00m rs \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mresponse_schemas]\n\u001b[0;32m---> 96\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mparse_and_check_json_markdown\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexpected_keys\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/output_parsers/json.py:163\u001b[0m, in \u001b[0;36mparse_and_check_json_markdown\u001b[0;34m(text, expected_keys)\u001b[0m\n\u001b[1;32m    151\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    152\u001b[0m \u001b[38;5;124;03mParse a JSON string from a Markdown string and check that it\u001b[39;00m\n\u001b[1;32m    153\u001b[0m \u001b[38;5;124;03mcontains the expected keys.\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    160\u001b[0m \u001b[38;5;124;03m    The parsed JSON object as a Python dictionary.\u001b[39;00m\n\u001b[1;32m    161\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    162\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 163\u001b[0m     json_obj \u001b[38;5;241m=\u001b[39m \u001b[43mparse_json_markdown\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    164\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m json\u001b[38;5;241m.\u001b[39mJSONDecodeError \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m    165\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m OutputParserException(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mGot invalid JSON object. Error: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00me\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/langchain/output_parsers/json.py:129\u001b[0m, in \u001b[0;36mparse_json_markdown\u001b[0;34m(json_string, parser)\u001b[0m\n\u001b[1;32m    119\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    120\u001b[0m \u001b[38;5;124;03mParse a JSON string from a Markdown string.\u001b[39;00m\n\u001b[1;32m    121\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    126\u001b[0m \u001b[38;5;124;03m    The parsed JSON object as a Python dictionary.\u001b[39;00m\n\u001b[1;32m    127\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    128\u001b[0m \u001b[38;5;66;03m# Try to find JSON string within triple backticks\u001b[39;00m\n\u001b[0;32m--> 129\u001b[0m match \u001b[38;5;241m=\u001b[39m \u001b[43mre\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msearch\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43mr\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m```(json)?(.*)```\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mjson_string\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mre\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mDOTALL\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    131\u001b[0m \u001b[38;5;66;03m# If no match found, assume the entire string is a JSON string\u001b[39;00m\n\u001b[1;32m    132\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m match \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/re/__init__.py:177\u001b[0m, in \u001b[0;36msearch\u001b[0;34m(pattern, string, flags)\u001b[0m\n\u001b[1;32m    174\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21msearch\u001b[39m(pattern, string, flags\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m):\n\u001b[1;32m    175\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Scan through string looking for a match to the pattern, returning\u001b[39;00m\n\u001b[1;32m    176\u001b[0m \u001b[38;5;124;03m    a Match object, or None if no match was found.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 177\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_compile\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpattern\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mflags\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msearch\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstring\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mTypeError\u001b[0m: expected string or bytes-like object, got 'dict'"
     ]
    }
   ],
   "source": [
    "template = \"\"\"\n",
    "    你扮演一个小学语文老师，帮助同学修改错别字。\n",
    "将用户输入的错别字进行修改，保证输出的文字是正确的。\n",
    "\n",
    "用户输入:\n",
    "{user_input}\n",
    "\n",
    "以 {format_instructions} 的格式输出\n",
    "\"\"\"\n",
    "\n",
    "prompt_template = PromptTemplate(\n",
    "    input_variables=[\"user_input\"],\n",
    "    partial_variables={\"format_instructions\":format_instructions},\n",
    "    template=template\n",
    ")\n",
    "final_prompt = prompt_template.format(user_input = \"今天天启真好，你先去那离玩吧\")\n",
    "\n",
    "response = llms.invoke(final_prompt)\n",
    "#大语言模型得到的结果\n",
    "print(response)\n",
    "#格式化结果\n",
    "\n",
    "output_parser.parse(response)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
